package com.my.mymafengwo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.smallstrong.mafengwopro.R;
import com.my.mymafengwo.base.BaseActivity;
import com.my.mymafengwo.fragment.BoradcastFragment;
import com.my.mymafengwo.fragment.EmptyFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by smallstrong on 2015/12/7.
 * step
 */
public class MineMessageActivity extends BaseActivity {
    private List<Fragment> list;
    private ViewPager viewPager;
    private RadioGroup rgMes;
    private RadioButton rbNotification, rbBroadcast, rbLetter;
    private ImageView ivLineLeft, ivLineRight, ivMiddle, ivBack;

    @Override
    protected void loadXml() {
        setContentView(R.layout.activity_mine_message);
    }

    @Override
    protected void getIntentData(Bundle savedInstanceState) {

    }

    @Override
    protected void initViews() {
        viewPager = (ViewPager) findViewById(R.id.vp);
        rgMes = (RadioGroup) findViewById(R.id.rg_login);
        ivLineLeft = (ImageView) findViewById(R.id.iv_line_left);
        ivLineRight = (ImageView) findViewById(R.id.iv_line_right);
        ivMiddle = (ImageView) findViewById(R.id.iv_line_middle);
        rbNotification = (RadioButton) findViewById(R.id.rb_notification);
        rbBroadcast = (RadioButton) findViewById(R.id.rb_broadcast);
        rbLetter = (RadioButton) findViewById(R.id.rb_letter);
        ivBack = (ImageView) findViewById(R.id.iv_icon_back_orange);
    }

    @Override
    protected void registerListener() {
        rgMes.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_notification:
                        viewPager.setCurrentItem(0);
                        ivLineLeft.setVisibility(View.VISIBLE);
                        ivMiddle.setVisibility(View.INVISIBLE);
                        ivLineRight.setVisibility(View.INVISIBLE);
                        Intent intent1 = new Intent(MineMessageActivity.this, MineLoginActivity.class);
                        startActivity(intent1);
                        break;

                    case R.id.rb_broadcast:
                        viewPager.setCurrentItem(1);
                        ivLineLeft.setVisibility(View.INVISIBLE);
                        ivMiddle.setVisibility(View.VISIBLE);
                        ivLineRight.setVisibility(View.INVISIBLE);
                        break;
                    case R.id.rb_letter:
                        viewPager.setCurrentItem(2);
                        ivLineLeft.setVisibility(View.INVISIBLE);
                        ivMiddle.setVisibility(View.INVISIBLE);
                        ivLineRight.setVisibility(View.VISIBLE);
                        Intent intent3 = new Intent(MineMessageActivity.this, MineLoginActivity.class);
                        startActivity(intent3);
                        break;
                }
            }
        });
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    rbNotification.setChecked(true);
                    ivLineLeft.setVisibility(View.VISIBLE);
                    ivMiddle.setVisibility(View.INVISIBLE);
                    ivLineRight.setVisibility(View.INVISIBLE);
                    Intent intent1 = new Intent(MineMessageActivity.this, MineLoginActivity.class);
                    startActivity(intent1);
                } else if (position == 1) {
                    rbBroadcast.setChecked(true);
                    ivLineLeft.setVisibility(View.INVISIBLE);
                    ivMiddle.setVisibility(View.VISIBLE);
                    ivLineRight.setVisibility(View.INVISIBLE);
                } else if (position == 2) {
                    rbLetter.setChecked(true);
                    ivLineLeft.setVisibility(View.INVISIBLE);
                    ivMiddle.setVisibility(View.INVISIBLE);
                    ivLineRight.setVisibility(View.VISIBLE);
                    Intent intent3 = new Intent(MineMessageActivity.this, MineLoginActivity.class);
                    startActivity(intent3);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() {
        list = new ArrayList<>();
        list.add(new EmptyFragment());
        list.add(new BoradcastFragment());
        list.add(new EmptyFragment());
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        viewPager.setCurrentItem(1);
    }

    class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}
